﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
using System.Text;
using System.Reflection;
using System.IO;


namespace GIupdate
{
    public partial class _Default : System.Web.UI.Page
    {
        public _Default()
        {
            this.Load += new EventHandler(Page_Load);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // We need at least the app id/name & the version -- v for version, a for app
            if (Request.QueryString["v"] != null && Request.QueryString["a"] != null)
            {
                // Construct the request xml
                StringBuilder sbResult = new StringBuilder();
                System.Xml.XmlWriterSettings oSettings = new System.Xml.XmlWriterSettings();
                oSettings.Encoding = Encoding.ASCII;
                oSettings.Indent = true;
                System.Xml.XmlWriter oWriter = System.Xml.XmlWriter.Create(sbResult, oSettings);
                oWriter.WriteStartElement("GIUpdateRequest");

                for (int i = 0; i < Request.QueryString.Count; i++)
                {
                    string sKeyName = Request.QueryString.GetKey(i);
                    string[] values = Request.QueryString.GetValues(i);
                    oWriter.WriteStartElement(sKeyName);        /* one key can have multiple values */
                    foreach (string V in values)
                    {
                        WriteNodeVale("Value", V, ref oWriter);
                    }
                    oWriter.WriteEndElement();
                }

                oWriter.WriteEndElement();
                oWriter.Close();

                //string sFolderPath = Assembly.GetAssembly(typeof(_Default)).Location;
                //sFolderPath = sFolderPath.Substring(0, sFolderPath.LastIndexOf(@"\") + 1);
                string sTarnsformerXSLTPath = Server.MapPath("UpdateRequestProcessor.xslt");
                //Response.Write("xslt path = " + sTarnsformerXSLTPath + " <br>");
                //string.Format("{0}UpdateRequestProcessor.xslt", sFolderPath);

                XmlDocument xd = new XmlDocument();

                // This will be replaced with xd.loadXml(...) call
                xd.LoadXml(sbResult.ToString());

                XslCompiledTransform xslt = new XslCompiledTransform();
                xslt.Load(sTarnsformerXSLTPath);
                StringBuilder sbOutput = new StringBuilder();
                StringWriter oOutputWriter = new StringWriter(sbOutput);
                xslt.Transform(xd, null, oOutputWriter);
                oOutputWriter.Close();

                Response.Write(sbOutput.ToString());
            }
            else
            {
                Response.Write("Error:No parameters supplied.");
            }
        }

        public void WriteNodeVale(string nodeName, string value, ref System.Xml.XmlWriter oWriter)
        {
            oWriter.WriteStartElement(nodeName);
            oWriter.WriteString(value);
            oWriter.WriteEndElement();
        }
    }
}
